package com.think.config;

import com.think.filter.ExceptionFilter;
import com.think.filter.MyFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.filter.DelegatingFilterProxy;

import javax.servlet.Filter;

@Configuration
public class MvcConfigurer {
    public MvcConfigurer() {
        System.out.println("spring容器启动初始化。。。");
    }

    //使用FastJsonAutoConfiguration代替
    /*@Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.QuoteFieldNames,
                SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.PrettyFormat
        );
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;
        return new HttpMessageConverters(converter);//优先级在最上面
    }*/

    /*
    这种方式需要implements WebMvcConfigurer
    并且converters.add的优先级最低
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = (MappingJackson2HttpMessageConverter) converters.get(7);
        converter.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
        System.out.println(converters);
    }*/

    @Bean
    public Filter myFilter() {
        return new MyFilter();
    }

    /**
     * 手动注册过滤器，它在容器启动的时候就调用了filter的init方法
     * 手动的可以设置filter的先后顺序order
     */
    @Bean
    public FilterRegistrationBean myFilterRegistration() {
        FilterRegistrationBean<Filter> registration = new FilterRegistrationBean();
        registration.setName("myFilter");
        //注意这里，filter中可以注入service，否则注入的service为空,myFilter在上面@Bean，不能在这里new
        registration.setFilter(new DelegatingFilterProxy("myFilter"));
        registration.addUrlPatterns("/xxx");
        return registration;
    }

    @Bean
    public ExceptionFilter exceptionFilter() {
        return new ExceptionFilter();
    }

    @Bean
    public FilterRegistrationBean registerExceptionFilter() {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(exceptionFilter());
        bean.addUrlPatterns("/*");
        bean.setOrder(1);
        return bean;
    }

    //必须返回spring自带的CorsFilter以支持oauth2,name必须为corsFilter,需要配置oauth HttpSecurity .cors()生效
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("http://192.168.44.249:5555");
        config.setAllowCredentials(true);
        config.addAllowedMethod("*");
        config.addAllowedHeader("*");
        //config.addExposedHeader("*");
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);
        return new CorsFilter(configSource);
    }
}
